package studio.wetrack.dinner.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import studio.wetrack.dinner.db.repo.ChopsticksRepo;
import studio.wetrack.dinner.domain.Chopsticks;
import studio.wetrack.dinner.service.ChopsticksService;

import java.util.List;

/**
 * Created by zhanghong on 17/7/22.
 */
@Service
public class ChopstickServiceImpl implements ChopsticksService {

    @Autowired
    ChopsticksRepo chopsticksRepo;

    public Chopsticks getChopsticks() {
        List<Chopsticks> list = chopsticksRepo.findByOccupied(false);
        if(list.size() == 0){
            return null;
        }else{
            Chopsticks chopsticks = list.get(0);
            chopsticks.setOccupied(true);
            chopsticksRepo.save(chopsticks);
            return chopsticks;
        }
    }

    @Override
    public void releaseChopsticks(Chopsticks chopsticks) {
        chopsticks.setOccupied(false);
        chopsticksRepo.save(chopsticks);
    }

}
